package com.itheima.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.PmsBrandMapper;
import com.itheima.pojo.PmsBrand;
import com.itheima.pojo.SmsHomeAdvertise;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import java.util.Arrays;
import java.util.List;

@DubboService
public class PmsBrandImpl implements PmsBrandApi{
    @Autowired
    private PmsBrandMapper pmsBrandMapper;
    /**
     * 添加品牌
     *
     * @param pmsBrand
     * @return
     */
    @Override
    public Boolean create(PmsBrand pmsBrand) {
        //先进行查询 品牌名称是否重复
        LambdaQueryWrapper<PmsBrand> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PmsBrand::getName,pmsBrand.getName());
        PmsBrand pmsBrand1 = pmsBrandMapper.selectOne(wrapper);
        //如果存在 则重复 返回 FALSE
        if(!ObjectUtils.isEmpty(pmsBrand1)){
            return false;
        }
        //走新增
        pmsBrandMapper.insert(pmsBrand);
        return true;
    }


    /**
     * 走批量删除
     *
     * @param ids
     */
    @Override
    public void delete(Integer[] ids) {
        pmsBrandMapper.deleteBatchIds(Arrays.asList(ids));
    }


    /**
     * 分页 模糊查询
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public IPage<PmsBrand> findPages(Integer pageNum, Integer pageSize, String keyword) {
        IPage<PmsBrand> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PmsBrand> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(!ObjectUtils.isEmpty(keyword),PmsBrand::getName,keyword);
        pmsBrandMapper.selectPage(page,wrapper);
        return page;
    }


    /**
     * 查询全部品牌
     */
    @Override
    public List<PmsBrand> listAll() {
        LambdaQueryWrapper<PmsBrand> wrapper=new LambdaQueryWrapper<>();
        return pmsBrandMapper.selectList(wrapper);
    }


    /**
     * 批量修改状态
     *
     * @param factoryStatus
     * @param ids
     */
    @Override
    public void update(Integer factoryStatus, Integer[] ids) {
        PmsBrand pmsBrand=new PmsBrand();
        pmsBrand.setFactoryStatus(factoryStatus);

        for (Integer id : ids) {
            pmsBrand.setId(id.longValue());
            pmsBrandMapper.updateById(pmsBrand);
        }
    }

    /**
     * 批量修改显示状态
     *
     * @param showStatus
     * @param ids
     */
    @Override
    public void updateShow(Integer showStatus, Integer[] ids) {
        PmsBrand pmsBrand=new PmsBrand();
        pmsBrand.setShowStatus(showStatus);

        for (Integer id : ids) {
            pmsBrand.setId(id.longValue());
            pmsBrandMapper.updateById(pmsBrand);
        }
    }


    /**
     * 根据id查询广告
     *
     * @param id
     * @return
     */
    @Override
    public PmsBrand findByID(Long id) {

        return pmsBrandMapper.selectById(id);
    }

    /**
     * 不做判断直接修改
     *
     * @param pmsBrand
     */
    @Override
    public void updateByIdNo(PmsBrand pmsBrand) {
        pmsBrandMapper.updateById(pmsBrand);
    }

    /**
     * 根据id 修改  需要判断是否有重复的品牌名
     *
     * @param pmsBrand
     * @return
     */
    @Override
    public Boolean updateById(PmsBrand pmsBrand) {
        //走进来 就不会出现自己和自己相同的情况
        //先查询 是否有重复的
        LambdaUpdateWrapper<PmsBrand> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(PmsBrand::getName,pmsBrand.getName());
        PmsBrand pmsBrand1 = pmsBrandMapper.selectOne(wrapper);
        if(pmsBrand1!=null){
            //重复
            return false;
        }
        pmsBrandMapper.updateById(pmsBrand);
        return true;

    }
}
